//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package org.jim.server.command.handler;

import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.jim.core.ImChannelContext;
import org.jim.core.ImPacket;
import org.jim.core.ImStatus;
import org.jim.core.config.ImConfig.Global;
import org.jim.core.exception.ImException;
import org.jim.core.message.MessageHelper;
import org.jim.core.packets.Command;
import org.jim.core.packets.MarkMessageReadReqBody;
import org.jim.core.packets.MarkMessageReadRespBody;
import org.jim.core.packets.RespBody;
import org.jim.core.packets.UserMessageData;
import org.jim.core.utils.JsonKit;
import org.jim.server.JimServerAPI;
import org.jim.server.command.AbstractCmdHandler;
import org.jim.server.config.ImServerConfig;
import org.jim.server.protocol.ProtocolManager.Converter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MarkMessageReadReqHandler extends AbstractCmdHandler {
    private static Logger logger = LoggerFactory.getLogger(MarkMessageReadReqHandler.class);

    public MarkMessageReadReqHandler() {
    }

    public Command command() {
        return Command.COMMAND_MARK_MESSAGE_READ_REQ;
    }

    public ImPacket handler(ImPacket packet, ImChannelContext imChannelContext) throws ImException {
        RespBody resPacket = new RespBody(Command.COMMAND_MARK_MESSAGE_READ_RESP, ImStatus.C10030);
        UserMessageData userMessageData = null;

        MarkMessageReadReqBody markMessageReadedReqBody;
        try {
            markMessageReadedReqBody = (MarkMessageReadReqBody)JsonKit.toBean(packet.getBody(), MarkMessageReadReqBody.class);
        } catch (Exception var14) {
            return this.getMessageFailedPacket(imChannelContext);
        }

        ImServerConfig imServerConfig = (ImServerConfig)Global.get();
        MessageHelper messageHelper = imServerConfig.getMessageHelper();
        String groupId = markMessageReadedReqBody.getGroupId();
        String userId = markMessageReadedReqBody.getUserId();
        String fromUserId = markMessageReadedReqBody.getFromUserId();
        logger.debug("markMessageReadReq=>,userId={},groupId={}", userId, groupId);
        boolean removed = true;
        if (!StringUtils.isEmpty(groupId)) {
            userMessageData = messageHelper.getGroupOfflineMessage(userId, groupId);
            messageHelper.removeGroupOfflineMessage(userId, groupId);
        } else if (!StringUtils.isEmpty(fromUserId)) {
            userMessageData = messageHelper.getFriendOfflineMessage(userId, fromUserId);
            messageHelper.removeFriendOfflineMessage(userId, fromUserId);
        } else {
            userMessageData = messageHelper.getUserOfflineMessage(userId);
            messageHelper.removeUserOfflineMessage(userId);
        }

        MarkMessageReadRespBody markMessageReadRespBody = MarkMessageReadRespBody.success();
        markMessageReadRespBody.setUserId(userId);
        markMessageReadRespBody.setFromUserId(fromUserId);
        markMessageReadRespBody.setGroupId(groupId);
        markMessageReadRespBody.setUserMessageData(userMessageData);
        logger.debug("MessageUnreadCountReqHandler=>,{}", JsonKit.toJSONString(markMessageReadRespBody));
        List<ImChannelContext> imChannelContextList = JimServerAPI.getByUserId(userId);
        if (imChannelContextList.size() > 1) {
            imChannelContextList.stream().filter((channelContext) -> {
                return channelContext.getSessionContext().getId() != imChannelContext.getSessionContext().getId();
            }).forEach((channelContext) -> {
                try {
                    markMessageReadRespBody.setSelf(false);
                    resPacket.setData(markMessageReadRespBody);
                    JimServerAPI.send(channelContext, Converter.respPacket(resPacket, channelContext));
                } catch (ImException var4) {
                    var4.printStackTrace();
                }

            });
        }

        markMessageReadRespBody.setSelf(true);
        resPacket.setData(markMessageReadRespBody);
        return Converter.respPacket(resPacket, imChannelContext);
    }

    public ImPacket getMessageFailedPacket(ImChannelContext imChannelContext) throws ImException {
        RespBody resPacket = new RespBody(Command.COMMAND_GET_MESSAGE_UNREAD_COUNT_RESP, ImStatus.C10029);
        return Converter.respPacket(resPacket, imChannelContext);
    }
}
